package dao;

import common.Group;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

public class AddGroup {
    //检查是否发送过加群信息
    public Boolean existGroup(Group y) {
        Connection conn=null;
        ResultSet res=null;
        try {
            Properties info=new Properties();
            info.load(new FileInputStream("src\\sql.properties"));
            String user=info.getProperty("user");
            String password=info.getProperty("password");
            String Driver=info.getProperty("Driver");
            String url=info.getProperty("url");
            //注册驱动
            Class.forName(Driver);
            //获取连接
            conn= DriverManager.getConnection(url,user,password);
            String sql="select * from groupp where group_id = ?";
            PreparedStatement presta = conn.prepareStatement(sql);
            presta.setString(1, y.getGroupid());

            res=presta.executeQuery();
            return res.next();
        }catch(SQLException e){
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return false;
    }

    public int getState(String userid, String groupid) {
        Connection conn=null;
        ResultSet res=null;
        int flag=-1;
        try {
            Properties info=new Properties();
            info.load(new FileInputStream("src\\sql.properties"));
            String user=info.getProperty("user");
            String password=info.getProperty("password");
            String Driver=info.getProperty("Driver");
            String url=info.getProperty("url");
            Class.forName(Driver);
            conn= DriverManager.getConnection(url,user,password);
            String sql="select * from grouplist where member_id=? and group_id=?";
            PreparedStatement presta = conn.prepareStatement(sql);
            presta.setString(1, userid);
            presta.setString(2, groupid);
            System.out.println(userid);
            System.out.println(groupid);
            res = presta.executeQuery();
            if (res.next()) {
                flag=res.getInt("statement");
                System.out.println(flag);
            }
        }catch(SQLException e){
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return flag;
    }

    //添加
    public boolean addGroup(Group u) {
        Connection conn=null;
        ResultSet res=null;
        try {
            Properties info=new Properties();
            info.load(new FileInputStream("src\\sql.properties"));
            String user=info.getProperty("user");
            String password=info.getProperty("password");
            String Driver=info.getProperty("Driver");
            String url=info.getProperty("url");
            Class.forName(Driver);
            conn= DriverManager.getConnection(url,user,password);
            String sql="insert into grouplist(group_id,member_id,statement)values(?,?,?)";
            PreparedStatement presta = conn.prepareStatement(sql);
            presta.setString(1, u.getGroupid());
            presta.setString(2, u.getuserid());
            presta.setInt(3, u.getGroupstate());
            int f=0;
            f=presta.executeUpdate();
            if(f!=0){
                return true;
            }else{
                return false;
            }
        }catch(SQLException e){
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return false;
    }
}
